//
//  LXJMapViewController.m
//  HMSport
//
//  Created by 李旭杰 on 2017/4/23.
//  Copyright © 2017年 LiXujie. All rights reserved.
//

#import "LXJMapViewController.h"
#import <AMapFoundationKit/AMapFoundationKit.h>

@interface LXJMapViewController ()<MAMapViewDelegate>
//记录是否添加起点大头针
@property (nonatomic, assign) BOOL hasSetStartAnno;

@end

@implementation LXJMapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ///初始化地图
    _mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    
    ///把地图添加至view
    [self.view addSubview:_mapView];
    
    //设置地图
    [self setupMapView];
}
- (void)setupMapView{

    //隐藏标尺
    _mapView.showsScale = NO;
    //关闭3D旋转
    _mapView.rotateCameraEnabled = NO;
    
    ///如果您需要进入地图就显示定位小蓝点，则需要下面两行代码
    _mapView.showsUserLocation = YES;
    _mapView.userTrackingMode = MAUserTrackingModeFollow;
    
    //开始后台运行 注意设置backgroundmode
    _mapView.allowsBackgroundLocationUpdates = YES;
    //取消自动暂停
    _mapView.pausesLocationUpdatesAutomatically = NO;
    
    //设置代理
    _mapView.delegate = self;
    
}

#pragma mark --- MAMapViewDelegate代理方法
//已经更新用户位置后调用
- (void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    
    //过滤掉方向改变触发的更新事件
    if (updatingLocation == NO) {
        
        return;
    }
    
    //位置更新后,发送通知给控制器  使用通知因为多个界面使用数据
    [[NSNotificationCenter defaultCenter] postNotificationName:@"LXJSportLocationDidUpdateNote" object:nil userInfo:nil];
    
    //更新位置后,设置地图中心点
    _mapView.centerCoordinate = userLocation.location.coordinate;
    
    
    //添加起点大头针
    if (_hasSetStartAnno == NO && self.track.startAnno) {
        
        [_mapView addAnnotation:self.track.startAnno];
        _hasSetStartAnno = YES;
    }
    
    
    
    //生成新的折线  每段起点都是上一段的终点
    LXJPolyline *newPolyline = [self.track appendPolylineWithDest:userLocation.location];
    
    //添加折线
    [_mapView addOverlay:newPolyline];
    
    NSLog(@"总距离:%f, 总时长:%f, 最大速度:%f, 平均速度:%f", self.track.totalDistance, self.track.totalTime, self.track.maxSpeed, self.track.avgSpeed);
}


//设置大头针视图时调用
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:reuseIndetifier];
        }
        //设置图片
        annotationView.image = [UIImage imageNamed:self.track.sportTypeImgName];
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height * 0.5);
        return annotationView;
    }
    return nil;
}

//设置覆盖物样式
- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[LXJPolyline class]])
    {
        MAPolylineRenderer *polylineRenderer = [[MAPolylineRenderer alloc] initWithPolyline:overlay];
        //线宽
        polylineRenderer.lineWidth    = 4.f;
        
        //描边颜色 根据折线的速度来改变
        LXJPolyline *polyline = overlay;
        
        polylineRenderer.strokeColor  = polyline.storkeColor;
        //拐点样式
        polylineRenderer.lineJoinType = kMALineJoinRound;
        //头尾样式
        polylineRenderer.lineCapType  = kMALineCapRound;
        
        return polylineRenderer;
    }
    
    return nil;
}
@end
